/**************************************************************************//**
\file  humidity.c

\brief This module is used for measurement humidity voltage.

\author
    Luis Alves\n


\internal
  History:
    24/07/2012 ljalves - Created
	
******************************************************************************/

/******************************************************************************
                   Includes section
******************************************************************************/
#include <humidity.h>
#include <adc.h>
#include <bspTaskManager.h>
#include <types.h>


/******************************************************************************
                   Define(s) section
******************************************************************************/
#define HUMIDITY_AMOUNT_DISPERSION       2
#define MAX_AMOUNT_COUNTER_H              20

/******************************************************************************
                   Types section
******************************************************************************/
// humidity states
typedef enum
{
  IDLE,      // idle
  BUSY,      // opened and ready to be used
  DATA       // performs request
} humidityStates_t;

// humidity descriptor
typedef struct
{
  uint16_t humidityData;
  BspHumidityCb_t humidityCallback; // callback
} HumidityControl_t;

/******************************************************************************
                   Global variables section
******************************************************************************/
// Monitors current state
static humidityStates_t humidityState = IDLE;
static HumidityControl_t humidityControl;
static HAL_AdcDescriptor_t adcDescriptor2;
//static uint8_t humExpectation;
//static uint8_t humAmountCnt;

/******************************************************************************
                   Implementations section
******************************************************************************/
/**************************************************************************//**
\brief Callback about ADC request was completed.
\param[in]
  data - result of ADC.
******************************************************************************/
void humidityCallback(void)
{
  /*humAmountCnt++;
  if ((humidityControl.humidityData > (humExpectation + HUMIDITY_AMOUNT_DISPERSION)) ||
      (humidityControl.humidityData < (humExpectation - HUMIDITY_AMOUNT_DISPERSION)))
  {
    humExpectation = humidityControl.humidityData;
    humAmountCnt = 0;
  }

  if (MAX_AMOUNT_COUNTER_H == humAmountCnt)
  {
    humAmountCnt = 0;
    humExpectation = 0;*/
    bspPostTask(BSP_HUMIDITY);
    return;/*
  }

  humidityState = BUSY;
  readHumidityData(humidityControl.humidityCallback);*/
}

/**************************************************************************//**
\brief Opens the component to use.
\return
  BC_SUCCESS - the component is ready to been use. \n
  BC_FAIL - otherwise.
******************************************************************************/
result_t openHumidity(void)
{
  if (IDLE == humidityState)
  {
    adcDescriptor2.tty = HAL_ADC_CHANNEL1;
    adcDescriptor2.resolution = RESOLUTION_10_BIT;
    adcDescriptor2.sampleRate = ADC_4800SPS;
    adcDescriptor2.voltageReference = INTERNAL_1d1V;
    adcDescriptor2.bufferPointer = &(humidityControl.humidityData);
    adcDescriptor2.selectionsAmount = 1;
    adcDescriptor2.callback = humidityCallback;
    humidityState = BUSY;
    HAL_OpenAdc(&adcDescriptor2);
    return BC_SUCCESS;
  }
  return BC_FAIL;
}

/**************************************************************************//**
\brief  Closes component.
\return
  BC_SUCCESS - always.
******************************************************************************/
result_t closeHumidity(void)
{
  if (IDLE == humidityState)
    return BC_FAIL;
  humidityState = IDLE;
  HAL_CloseAdc(&adcDescriptor2);
  return BC_SUCCESS;
}

/**************************************************************************//**
\brief  Starts ADC request on humidity channel.
\param[in]
    data - sensor data.
\return
    BC_FAIL - battery component was not opened. \n
    BC_SUCCESS - other case.
******************************************************************************/
result_t readHumidityData(void (*f)(bool result, int16_t data))
{
  //if (BUSY != humidityState)
  //  return BC_FAIL;
  
  //if (!f)
  //  return BC_FAIL;
  
  humidityState = DATA;
  humidityControl.humidityCallback = f;
  HAL_ReadAdc(&adcDescriptor2, HAL_ADC_CHANNEL1);
  return BC_SUCCESS;
}

/**************************************************************************//**
\brief BSP battery handler.
******************************************************************************/
void bspHumidityHandler(void)
{
  int16_t value;

  // 1100 - 1.1 V reference voltage expressed in mV (1110 mV)
  // 3 - battery voltage is divided by 3 on the AlceSense board
  // 256 - steps for 8 bit ADC
  // Resulting value is a true humidity voltage expressed in mV
  value = (humidityControl.humidityData * 1100ul * 3ul) / 1024ul;

  humidityState = BUSY;
  humidityControl.humidityCallback(true, value);
}


// eof battery.c
